/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package crawler;

import java.io.IOException;
import java.util.Iterator;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

/**
 *
 * @author Sasi-Admin
 */
public class MovieCrawler implements Runnable {

    Document searchDoc;
    private String URL;
    public static Executor ex;
    
    public MovieCrawler(String URL){
        this.setURL(URL);
        this.init();
    }
    
    public static void main(String[] args) {
        MovieCrawler m = new MovieCrawler("http://www.imdb.com/search/title?genres=action&sort=user_rating,desc&start=1&title_type=feature");
        m.crawl();
            
        

        

    }

    public void crawl(){
        ex.execute(this);
    }
    public void setURL(String URL){
        this.URL = URL;
    }

    public void connect() throws IOException {
        searchDoc = Jsoup.connect(URL).userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.120 Safari/535.2").timeout(10 * 1000).get();
    }
    
    public void init(){
        if(ex == null){
            ex = Executors.newCachedThreadPool();
        }
    }
    
    @Override
    public void run() {
        try {
            this.connect();
            Elements rows = searchDoc.select("td[class*=title]");

            for (Iterator<Element> it = rows.iterator(); it.hasNext();) {
                Element element = it.next();

                if (element != null) {
                    String Name = element.child(1).text();
                    String URL = element.child(1).absUrl("href");
                    String Rating = element.select("span[class*=rating-rating]").text();
                    System.out.println(Name + " " + URL);
                }

            }
        } catch (IOException ex) {
            Logger.getLogger(MovieCrawler.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
